package com.t2two.example.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.db.Entity;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.t2two.example.common.CustomException;
import com.t2two.example.convert.ConvertUserInfo;
import com.t2two.example.entity.userinfo.UserInfo;
import com.t2two.example.mapper.userinfo.UserInfoMapper;
import com.t2two.example.service.UserInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @description: TODO
 * @author: lifei
 * @date: 2023/6/26 12:18
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;
//    @Resource
//    private JavaMailSender javaMailSender;
    @Override
    public int insertUserInfo(UserInfo userInfo) {
        throw new CustomException("200","adfsdfdsfdsfsdf");
//        return userInfoMapper.insertUserInfo(userInfo);
    }
    @Override
    public String queryById(Long guid) {
        String jsonString = JSON.toJSONString(userInfoMapper.queryByPrimaryKey(guid));
        Entity userInfo = parseData("user_info", jsonString);

        return JSON.toJSONString(userInfoMapper.queryByPrimaryKey(guid));
    }

    public Entity parseData(String tableName, Object data){
        Entity entity = new Entity();
        entity.setTableName(tableName);

        return entity;
    }

    @Override
    public void test(HttpServletResponse response){
        List<UserInfo> userInfoList = ConvertUserInfo.getUserInfoList();
        try (ExcelWriter writer = ExcelUtil.getWriter()){
            // 表头处理
            List<String> rowHead = CollUtil.newLinkedList("序号","主键", "姓名", "金额", "年龄", "唯一标识","标识","双精度","日期时间", "备注");
            List<List<Object>> rows = new LinkedList<>();
            // 第二行表头
            LinkedList<Object> header2 = CollUtil.newLinkedList();
            for (int i = 0; i < 8; i++) {
                header2.add("");
            }
            header2.add("创建时间");
            header2.add("修改时间");
            header2.add("time时间");
            header2.add("备注");
            // 标题
            writer.merge(header2.size() - 1, "统计表测试");
            // 写入表头
            writer.writeHeadRow(rowHead);
            writer.writeHeadRow(header2);
            // 表头合并
            for (int i = 0; i < header2.size(); i++) {
                if (i < 8) {
                    writer.merge(1, 2, i, i, null, false);
                } else if (i == header2.size() - 1) {
                    writer.merge(1, 2, i, i, "备注", true);
                }
            }
            writer.merge(1, 1, 8, header2.size() - 2, null, false);
            if(CollectionUtils.isNotEmpty(userInfoList)){
                for (int i = 0; i < userInfoList.size(); i++) {
                    UserInfo userInfo = userInfoList.get(i);
                    LinkedList<Object> row = CollUtil.newLinkedList(i + 1, userInfo.getId(), userInfo.getName(), userInfo.getMoney(), userInfo.getAge(), userInfo.getGuid(), userInfo.getFlag(), userInfo.getDoubleMoney());
                    row.add(userInfo.getCreateDate());
                    row.add(userInfo.getUpdateDate());
                    row.add(userInfo.getTimes());
                    row.add(userInfo.getMemo());
                    rows.add(row);
                }
            }
            writer.write(rows,Boolean.TRUE);
            // 设置宽高
            writer.setColumnWidth(0, 8);
            for (int i = 1; i < header2.size() - 1; i++) {
                writer.setColumnWidth(i, 10);
            }
            writer.setColumnWidth(1, 15);
            writer.setColumnWidth(header2.size() - 1, 15);
            writer.setRowHeight(0, 40);
            for (int i = 1; i < userInfoList.size() + 3; i++) {
                writer.setRowHeight(i, 25);
            }
            //自定义标题别名
            //response为HttpServletResponse对象
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setCharacterEncoding("UTF-8");
            // 弹出下载对话框的文件名，中文请自行编码
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode( "统计表测试", "utf-8") + ".xlsx");
            ServletOutputStream out = response.getOutputStream();
            writer.flush(out);
        }catch (Exception e){

        }
    }

    @Override
    public Map sendEmail() {
//        JavaMailSenderImpl sender = new JavaMailSenderImpl();
//        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        return null;
    }
}
